<?php

namespace Admin\Controller;

class ArticleController extends BaseController {

    public function add() {

        $sort = D('Sort');
        if (IS_GET) {
            $result = $sort->where('status=0')->select();
            $data = $sort->findSubTree($result, 0, 1);
            $data = $sort->tree($data);
            $this->assign('data', $data);
            $this->display();
        } else {
            $data = I('post.');
            $data['addtime'] = date('Y-m-d H:i:s', time());
            $article = D('Article');
            if ($article->create($data) && $article->add()) {
                $this->success('成功');
            } else {
                $this->error($article->getError());
            }
        }
    }

    /**
     * 显示文章列表
     * @return [type] [description]
     */
    public function lists() {
        $article = D('article');
        if (!IS_AJAX) {
            $this->display();
        } else {

            $res = I('get.');
            $data = $article->getList($res);
            $output = [
                "iTotalRecords" => $data['count'], //总数
                "iTotalDisplayRecords" => $data['count'], //总页数
                "aaData" => $data['list']
            ];
            echo json_encode($output);
        }
    }

    //修改顶置
    public function is_top() {
        $condition = I('get.');
        $article = D('article');
        $res = $article->upDateByIsTop($condition);
        // var_dump($condition);die;
        if ($res) {
            $data['status'] = '1';
            $data['msg'] = '成功';
            $this->ajaxReturn($data);
        } else {
            $data['status'] = '0';
            $data['msg'] = '排序值没有变化';
            $this->ajaxReturn($data);
        }
    }

    /**
     * 查看文章详情
     */
    public function info() {
        //接受文章id 
        $id = I('get.id');
        // var_dump($id);exit;
        $article = D('article');
        $data = $article->ArticleInfo($id);
        $res = $data[0]['content'];
        $this->assign('res', $res);
        $this->display();
        // $this->ajaxReturn($data);
    }


    public function seekType(){
        $seek_type = I('get.seek_type');
        var_dump($seek_type);
    }
}
